package com.tanhua.admin.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.admin.exception.BusinessException;
import com.tanhua.admin.interceptor.AdminHolder;
import com.tanhua.admin.mapper.AdminMapper;
import com.tanhua.commons.utils.Constants;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.admin.Admin;
import com.tanhua.model.vo.AdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 登录
     * @param username
     * @param password
     * @param verificationCode
     * @param uuid
     * @return
     */
    public ResponseEntity login(String username, String password, String verificationCode, String uuid) {
        //判断账号和密码是否为空
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            //用户名或者密码为空
            //throw new BusinessException("用户名或者密码为空");
            Map map1 = new HashMap();
            map1.put("message", "用户名或者密码为空");
            return ResponseEntity.status(500).body(map1);
        }
        //3、判断验证码是否正确
        if (StringUtils.isEmpty(verificationCode)) {
            //验证码为空
            throw new BusinessException("验证码为空");
        }
        //从redis中获取验证码
        String key = Constants.CAP_CODE + uuid;
        String code = redisTemplate.opsForValue().get(key);

        if (StringUtils.isEmpty(code) || !code.equals(verificationCode)) {
            //验证码错误
            throw new BusinessException("验证码错误");
        }
        //删除key
        redisTemplate.delete(key);
        //4、根据用户名查询用户
        LambdaQueryWrapper<Admin> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Admin::getUsername,username);
        Admin admin = adminMapper.selectOne(lqw);
        //5、判断用户是否存在，密码是否一致
        password = SecureUtil.md5(password);
        if(ObjectUtil.isEmpty(admin)||!admin.getPassword().equals(password)){
            //用户名错误或者密码不一致
            throw new BusinessException("密码错误");
        }
        Map<String, Object> claims = new HashMap<String, Object>();
        claims.put("username", username);
        claims.put("id", admin.getId());
        //6、通过JWT生成token
        String token = JwtUtils.getToken(claims);
        Map res = new HashMap();
        res.put("token", token);
        //8、构造返回值
        return ResponseEntity.ok(res);
    }

    /**
     * 获取用户信息
     * @return
     */
    public AdminVo profile() {
        Long userId = AdminHolder.getUserId();
        Admin admin = adminMapper.selectById(userId);
        return AdminVo.init(admin);
    }
}
